package com.example.demo;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//该注解表示这个类是一个切面
@Aspect
//该注解表示该类随着项目的启动而启动
@Component
public class UserAspect {
    //定义切点,配置拦截规则
    @Pointcut("execution(* com.example.demo.controller.*.*(..))")
    public void pointcut() {

    }
    @Before("pointcut()")
    public void beforeAdvice() {

        System.out.println("我是前置通知");
    }
    @After("pointcut()")
    public void afterAdvice() {

        System.out.println("我是后置通知");
    }
    // 环绕⽅法
    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint){
        Object obj = null;
        System.out.println("Around ⽅法开始执⾏");
        try {
            // 执⾏拦截⽅法
            obj = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("Around ⽅法结束执⾏");
        return obj;
    }

}
